/*!
    \file  gd32f10x_it.c
    \brief interrupt service routines
*/

/*
    Copyright (C) 2017 GigaDevice

    2014-12-26, V1.0.0, firmware for GD32F10x
    2017-06-20, V2.0.0, firmware for GD32F10x
*/

#include "gd32f10x_it.h"
#include "gd32f10x_eval.h"
//extern can_receive_message_struct receive_message;
//extern FlagStatus can0_receive_flag;
//extern FlagStatus can1_receive_flag;
//extern FlagStatus can0_error_flag;
//extern FlagStatus can1_error_flag;
uint8_t i = 0, temp = 0, data;
/*!
    \brief      this function handles NMI exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
extern uint8_t rxbuffer[10];
extern __IO uint16_t rxcount;
extern uint8_t rx_size;
void USART0_IRQHandler(void)
{
    if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
    {
        temp = usart_data_receive(USART0);
        printf("%c", temp);
        if (temp != 90 && temp != 91 && temp != 92 && temp != 93 && temp != 94)
        {
            data = temp;
        }
        else
        {
            rxbuffer[temp - 90] = data;
        }

        /* receive data */
        // rxbuffer[rxcount++] = usart_data_receive(USART0);
        //for (i = 0; i < 7; ++i)
        //{
        //    printf("%c", rxbuffer[i]);
        //}
        //if (rxcount == rx_size)
        //{
        //    usart_interrupt_disable(USART0, USART_INT_RBNE);
        //}
    }
    //    if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE)){
    //        /* transmit data */
    //        usart_data_transmit(USART0, txbuffer[txcount++]);
    //        if(txcount == tx_size){
    //            usart_interrupt_disable(USART0, USART_INT_TBE);
    //        }
    //    }
}


void NMI_Handler(void)
{
}

/*!
    \brief      this function handles HardFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void HardFault_Handler(void)
{
    /* if Hard Fault exception occurs, go to infinite loop */
    while (1)
    {
    }
}

/*!
    \brief      this function handles MemManage exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void MemManage_Handler(void)
{
    /* if Memory Manage exception occurs, go to infinite loop */
    while (1)
    {
    }
}

/*!
    \brief      this function handles BusFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void BusFault_Handler(void)
{
    /* if Bus Fault exception occurs, go to infinite loop */
    while (1)
    {
    }
}

/*!
    \brief      this function handles UsageFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void UsageFault_Handler(void)
{
    /* if Usage Fault exception occurs, go to infinite loop */
    while (1)
    {
    }
}

/*!
    \brief      this function handles SVC exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void SVC_Handler(void)
{
}

/*!
    \brief      this function handles DebugMon exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DebugMon_Handler(void)
{
}

/*!
    \brief      this function handles PendSV exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void PendSV_Handler(void)
{
}

/*!
    \brief      this function handles SysTick exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
//void SysTick_Handler(void)
//{
//}

/*!
    \brief      this function handles CAN0 RX0 exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
//void CAN0_RX0_IRQHandler(void)
//{
//    /* check the receive message */
//    can_message_receive(CAN0, CAN_FIFO0, &receive_message);
//
//    if((0x300>>1 == receive_message.rx_sfid)&&(CAN_FF_STANDARD == receive_message.rx_ff)&&(2 == receive_message.rx_dlen)){
//        can0_receive_flag = SET;
//    }else{
//        can0_error_flag = SET;
//    }
//}
///*!
//    \brief      this function handles CAN1 RX0 exception
//    \param[in]  none
//    \param[out] none
//    \retval     none
//*/
//void CAN1_RX0_IRQHandler(void)
//{
//    /* check the receive message */
//    can_message_receive(CAN1, CAN_FIFO0, &receive_message);
//
//    if((0x300>>1 == receive_message.rx_sfid)&&(CAN_FF_STANDARD == receive_message.rx_ff)&&(2 == receive_message.rx_dlen)){
//        can1_receive_flag = SET;
//    }else{
//        can1_error_flag = SET;
//    }
//}
